import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        name: "welcome",
        component: () => import("../views/welcome.vue"),
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/login.vue"),
  },
];

const router = new VueRouter({
  routes,
});


//此步为导航守卫，未登录无法跳转其他页面
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {//1如果当前页为登录页面，则继续执行下一步
    next();
  } else {//2否则
    if (sessionStorage.getItem("token")) {//2.1如果获取到token的话，继续执行下一步
      next();
    } else {//2.2否则，则去登录页面进行正常登录
      next("/login");
    }
  }
});

export default router;
